package com.leetcode.周赛.第87场;

/**
 * @author: ZhouBert
 * @date: 2021/1/22
 * @description: 845. 数组中的最长山脉
 * https://leetcode-cn.com/problems/longest-mountain-in-array/
 */
public class B_845_数组中的最长山脉 {


	public static void main(String[] args) {
		int[] arr = new int[]{2, 1, 4, 7, 3, 2, 5};
		//arr = new int[]{2, 2, 2};
		arr = new int[]{0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
		arr = new int[]{875, 884, 239, 731, 723, 685};
		B_845_数组中的最长山脉 action = new B_845_数组中的最长山脉();
		int res = action.longestMountain(arr);
		System.out.println("res = " + res);
	}

	public int longestMountain(int[] arr) {
		//记录最长山峰的宽度
		int res = 0;
		int begin = 0, end = 1, preEnd = 0;
		int len = arr.length;
		//平峰- 0；上峰- 1；下峰- -1
		int lable = 0;
		int tempWidth = 0;
		while (end < len) {
			int compare = arr[preEnd] == arr[end] ? 0 : (arr[preEnd] < arr[end] ? -1 : 1);
			//1.平地
			if (lable == 0) {
				if (compare == -1) {
					//上峰
					lable = 1;
					tempWidth++;
				} else {
					//平 || 下
					tempWidth = 0;
				}
			} else if (lable == -1) {
				//2.下峰
				if (compare == 1) {
					//继续下峰
					tempWidth++;
				} else if (compare == 0) {
					//平
					res = Math.max(tempWidth + 1, res);
					tempWidth = 0;
					lable = 0;
				} else {
					//上
					res = Math.max(tempWidth + 1, res);
					tempWidth = 1;
					lable = 1;
				}
			} else {
				//3.上峰
				if (compare == -1) {
					//继续上
					tempWidth++;
				} else if (compare == 0) {
					//平
					tempWidth = 0;
					lable = 0;
				} else {
					//下
					tempWidth++;
					lable = -1;
				}
			}

			preEnd = end;
			end++;
		}
		//最后一直下坡
		if (lable == -1 && end == len) {
			res = Math.max(tempWidth + 1, res);
		}
		return res;
	}
}
